﻿/*
 * Created by SharpDevelop.
 * User: phtrung
 * Date: 4/29/2009
 * Time: 10:37 AM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using System.Collections;
using System.Text;

namespace popLib
{
	/// <summary>
	/// Description of APILib.
	/// </summary>
	public class APILib
	{
		
		
		[DllImport("user32.dll", SetLastError = true)]
		public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);
		
		[DllImport("User32.dll")]
		public static extern Int32 SetForegroundWindow(IntPtr hWnd);		
		
		[DllImport("user32.dll")]
		public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
		
		
		[DllImportAttribute("user32.dll")]
		public static extern IntPtr SendMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
		
		
		
		[DllImportAttribute("user32.dll")]
		public static extern IntPtr PostMessage(IntPtr hWnd, int Msg, int wParam, int lParam);
		
		[DllImportAttribute("user32.dll")]
		public static extern bool ReleaseCapture();
				
		
		#region keycode
		public const int WM_SETHOTKEY = 50;
		public const int WM_SHOWWINDOW = 24;
		public const int HK_SHIFTA = 321;   // Shift + A
		public const int HK_SHIFTB = 322;   // Shift + B
		public const int HK_SHIFTZ = 346;	 // Shift + Z	
		public const int HK_CONTROLA = 577; // Control + A
		public const int HK_ALTZ = 1114;	 // ALT + Z	
		
			
		public const int VK_Alt = 18;
		public const int VK_Tab = 9;
		public const int VK_Backspace = 8;		
		public const int VK_Ctrl = 17;
		public const int VK_Shift = 16;
		public const int VK_V = 86;
		public const int VK_A = 65;
		public const int VK_B = 66;
		public const int VK_Z = 90;
		
		public const int KEYEVENTF_EXTENDEDKEY = 0x01;
		public const int KEYEVENTF_KEYDOWN = 0x00;
		public const int KEYEVENTF_KEYUP = 0x02;
		
		public enum VK : int
		{
			SHIFT        = 0x10,
			CONTROL      = 0x11,
			CONTROLKEY   = 0x11,
			CTRL         = 0x11,
			ALT			 = 0x12,
			MENU         = 0x12,
			ESCAPE       = 0x1B,
			BACK         = 0x08,
			BACKSPC      = 0x08,
			TAB          = 0x09,
			RETURN       = 0x0D,
			ENTER        = 0x0D,
			PRIOR        = 0x21,
			NEXT         = 0x22,
			END          = 0x23,
			HOME         = 0x24,
			LEFT         = 0x25,
			UP           = 0x26,
			RIGHT        = 0x27,
			DOWN         = 0x28,
			SELECT       = 0x29,
			PRINT        = 0x2A,			
			EXECUTE      = 0x2B,
			SNAPSHOT     = 0x2C,
			PRINTSCREEN  = 0x2C,
			INSERT       = 0x2D,
			DELETE       = 0x2E,
			DEL          = 0x2E,
			HELP         = 0x2F,
			NUMPAD0      = 0x60,
			NUMPAD1      = 0x61,
			NUMPAD2      = 0x62,
			NUMPAD3      = 0x63,
			NUMPAD4      = 0x64,
			NUMPAD5      = 0x65,
			NUMPAD6      = 0x66,
			NUMPAD7      = 0x67,
			NUMPAD8      = 0x68,
			NUMPAD9      = 0x69,
			MULTIPLY     = 0x6A,
			ADD          = 0x6B,
			SEPARATOR    = 0x6C,
			SUBTRACT     = 0x6D,
			DECIMAL      = 0x6E,
			DIVIDE       = 0x6F,
			F1           = 0x70,
			F2           = 0x71,
			F3           = 0x72,
			F4           = 0x73,
			F5           = 0x74,
			F6           = 0x75,
			F7           = 0x76,
			F8           = 0x77,
			F9           = 0x78,
			F10          = 0x79,
			F11          = 0x7A,
			F12          = 0x7B,
			OEM_1        = 0xBA,   // ',:' for US
			OEM_PLUS     = 0xBB,   // '+' any country
			OEM_COMMA    = 0xBC,   // ',' any country
			OEM_MINUS    = 0xBD,   // '-' any country
			OEM_PERIOD   = 0xBE,   // '.' any country
			OEM_2        = 0xBF,   // '/?' for US
			OEM_3        = 0xC0,   // '`~' for US
			MEDIA_NEXT_TRACK     = 0xB0,
			MEDIA_PREV_TRACK     = 0xB1,
			MEDIA_STOP           = 0xB2,
			MEDIA_PLAY_PAUSE     = 0xB3,
			LWIN       =0x5B,
			WIN        =0x5B,
			RWIN       =0x5C
		}
		
		public enum MouseEventFlags
	    {
	        LEFTDOWN = 0x00000002,
	        LEFTUP = 0x00000004,
	        MIDDLEDOWN = 0x00000020,
	        MIDDLEUP = 0x00000040,
	        MOVE = 0x00000001,
	        ABSOLUTE = 0x00008000,
	        RIGHTDOWN = 0x00000008,
	        RIGHTUP = 0x00000010
	    }
		#endregion
						
        [DllImport("user32.dll")]
        public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
        
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr hWnd, Int32 Msg, IntPtr wParam, IntPtr lParam);
        
        [DllImport("user32.dll")]
        public static extern IntPtr SetActiveWindow(IntPtr hWnd );
		
        [DllImport("user32.dll")]
        public static extern IntPtr SetFocus(IntPtr hWnd );
        
        [DllImport("user32.dll", EntryPoint="DefWindowProcA")]
   		public static extern IntPtr DefWindowProc(IntPtr hwnd, int wMsg, int wParam, int lParam);
		
   		[DllImport("user32.dll")]
    	public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);
    	   	
    	[DllImport("user32.dll",CharSet=CharSet.Auto, CallingConvention=CallingConvention.StdCall)]
		public static extern void mouse_event(long dwFlags, long dx, long dy, long cButtons, long dwExtraInfo);
    	
    	[DllImport("user32.dll")]
		public static extern IntPtr GetAsyncKeyState(int nVirtKey );
    	
				
		// hide form from alt+tab dialog
		[DllImport("user32", CharSet=CharSet.Auto)]
		public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
		
		[DllImport("user32", CharSet=CharSet.Auto)]
		public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
    			
		[DllImport("user32.dll")]
        public static extern IntPtr SetParent(IntPtr hWndChild, IntPtr hWndNewParent);
        
        [DllImport("kernel32.dll", SetLastError=true)]
		public static extern bool SetSystemTime( [In] ref SYSTEMTIME st );	
		
		public struct SYSTEMTIME 
		{
			public short wYear;
			public short wMonth;
			public short wDayOfWeek;
			public short wDay;
			public short wHour;
			public short wMinute;
			public short wSecond;
			public short wMilliseconds;
		}

		
		[DllImport("kernel32", SetLastError=true)]
        public static extern int WritePrivateProfileString(string section, string key, string value, string fileName);        
        [DllImport("kernel32", SetLastError=true)]
		public static extern int WritePrivateProfileString(string section, string key, int value, string fileName);
        [DllImport("kernel32", SetLastError=true)]
        public static extern int WritePrivateProfileString(string section, int key, string value, string fileName);
        [DllImport("kernel32", SetLastError=true)]
        public static extern int WritePrivateProfileStringW(string section, string key, string value, string fileName);        
        [DllImport("kernel32", SetLastError=true)]
		public static extern int WritePrivateProfileStringW(string section, string key, int value, string fileName);
        [DllImport("kernel32", SetLastError=true)]
        public static extern int WritePrivateProfileStringW(string section, int key, string value, string fileName);        
        
        
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder result, int size, string fileName);
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(string section, int key, string defaultValue, [MarshalAs(UnmanagedType.LPArray)] byte[] result, int size, string fileName);
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileString(int section, string key, string defaultValue, [MarshalAs(UnmanagedType.LPArray)] byte[] result, int size, string fileName);
        
		[DllImport("kernel32")]
        public static extern int GetPrivateProfileStringW(string section, string key, string defaultValue, StringBuilder result, int size, string fileName);
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileStringW(string section, int key, string defaultValue, [MarshalAs(UnmanagedType.LPArray)] byte[] result, int size, string fileName);
        [DllImport("kernel32")]
        public static extern int GetPrivateProfileStringW(int section, string key, string defaultValue, [MarshalAs(UnmanagedType.LPArray)] byte[] result, int size, string fileName);
        
		[DllImport("winmm.dll")]
		public static extern long mciSendString(string strCommand, StringBuilder strReturn, int iReturnLength, IntPtr hwndCallback);
		
		[DllImport("user32.dll")]
        public static extern void LockWorkStation();
        [DllImport("user32.dll")]
        public static extern int ExitWindowsEx(int uFlags, int dwReason);
        
        [DllImport("wininet.dll")]
		public extern static bool InternetGetConnectedState(out int Description, int ReservedValue);
              
		
		[DllImport("user32.dll", CharSet=CharSet.Auto)]
		public static extern IntPtr DefWindowProc(IntPtr hWnd, int msg, IntPtr wParam, IntPtr lParam);
	}
	
	#region enum ShowWindowStyle
	public enum ShowWindowStyle : int
	{
	    /// <summary>Hides the window and activates another window.</summary>
	    /// <remarks>See SW_HIDE</remarks>
	    Hide = 0,
	    /// <summary>Activates and displays a window. If the window is minimized 
	    /// or maximized, the system restores it to its original size and 
	    /// position. An application should specify this flag when displaying 
	    /// the window for the first time.</summary>
	    /// <remarks>See SW_SHOWNORMAL</remarks>
	    ShowNormal = 1,
	    /// <summary>Activates the window and displays it as a minimized window.</summary>
	    /// <remarks>See SW_SHOWMINIMIZED</remarks>
	    ShowMinimized = 2,
	    /// <summary>Activates the window and displays it as a maximized window.</summary>
	    /// <remarks>See SW_SHOWMAXIMIZED</remarks>
	    ShowMaximized = 3,
	    /// <summary>Maximizes the specified window.</summary>
	    /// <remarks>See SW_MAXIMIZE</remarks>
	    Maximize = 3,
	    /// <summary>Displays a window in its most recent size and position. 
	    /// This value is similar to "ShowNormal", except the window is not 
	    /// actived.</summary>
	    /// <remarks>See SW_SHOWNOACTIVATE</remarks>
	    ShowNormalNoActivate = 4,
	    /// <summary>Activates the window and displays it in its current size 
	    /// and position.</summary>
	    /// <remarks>See SW_SHOW</remarks>
	    Show = 5,
	    /// <summary>Minimizes the specified window and activates the next 
	    /// top-level window in the Z order.</summary>
	    /// <remarks>See SW_MINIMIZE</remarks>
	    Minimize = 6,
	      /// <summary>Displays the window as a minimized window. This value is 
	      /// similar to "ShowMinimized", except the window is not activated.</summary>
	    /// <remarks>See SW_SHOWMINNOACTIVE</remarks>
	    ShowMinNoActivate = 7,
	    /// <summary>Displays the window in its current size and position. This 
	    /// value is similar to "Show", except the window is not activated.</summary>
	    /// <remarks>See SW_SHOWNA</remarks>
	    ShowNoActivate = 8,
	    /// <summary>Activates and displays the window. If the window is 
	    /// minimized or maximized, the system restores it to its original size 
	    /// and position. An application should specify this flag when restoring 
	    /// a minimized window.</summary>
	    /// <remarks>See SW_RESTORE</remarks>
	    Restore = 9,
	    /// <summary>Sets the show state based on the SW_ value specified in the 
	    /// STARTUPINFO structure passed to the CreateProcess function by the 
	    /// program that started the application.</summary>
	    /// <remarks>See SW_SHOWDEFAULT</remarks>
	    ShowDefault = 10,
	    /// <summary>Windows 2000/XP: Minimizes a window, even if the thread 
	    /// that owns the window is hung. This flag should only be used when 
	    /// minimizing windows from a different thread.</summary>
	    /// <remarks>See SW_FORCEMINIMIZE</remarks>
	    ForceMinimized = 11
	}
	#endregion
	
	#region GetWindow_Cmd
	public enum GetWindow_Cmd : uint {
	    GW_HWNDFIRST = 0,
	    GW_HWNDLAST = 1,
	    GW_HWNDNEXT = 2,
	    GW_HWNDPREV = 3,
	    GW_OWNER = 4,
	    GW_CHILD = 5,
	    GW_ENABLEDPOPUP = 6
	}
	#endregion
	
	#region enum PostMessageCode
	public enum PostMessageCode:int{
		WM_QUIT = 0x12,
		WM_CLOSE = 0x10,
		WM_DESTROY = 0x2,
		WM_NCDESTROY = 0x82			
	}
	#endregion
	
	#region enum SendMessageCode
	public enum SendMessageCode:int{
		WM_COMMAND = 0x0112,
		WM_CLOSE = 0xF060
	}
	#endregion
}
